﻿Imports ProjectWIFE.Exceptions
Namespace TypesOfLine
 Public Class INI_Section
  Inherits INI_Base
  Protected mSectionTitle As String = Nothing
  Protected mParameters As New List(Of INI_Base)
  Public Property SectionName() As String
   Get
    Return mSectionTitle
   End Get
   Set(ByVal value As String)
    mSectionTitle = value
   End Set
  End Property

#Region "New"
  Public Sub New(ByVal SectionTitle As String, _
                 Optional ByVal Comment As String = Nothing)
   MyBase.New(Nothing, Comment)
   Me.Section = Me
   mSectionTitle = SectionTitle
  End Sub
#End Region

  Public Sub Tidy()
   Console.WriteLine("Tidy [{0}]", mSectionTitle)
   Dim er = From i As INI_Parameter In mParameters Order By i.Key Ascending
   mParameters = er.Cast(Of INI_Base)().ToList
  End Sub

  Public Function TryParameter(ByVal Parameter As String, ByRef ReturnedParameter As INI_Parameter) As Boolean
   Dim success As Boolean = True
   Try
    ReturnedParameter = Me(Parameter)
   Catch ex As Exception
    success = False
   End Try
   Return success
  End Function

  Public Function AddParameterReturn(ByVal ParameterName As String, Optional ByVal Value As String = Nothing, Optional ByVal Comment As String = Nothing) As INI_Parameter
   Dim rt As INI_Parameter = Nothing
   If TryParameter(ParameterName, rt) = False Then
    ' Section doesn't exist
    Dim NewIniParam = New INI_Parameter(Me, ParameterName, , Comment)
    mParameters.Add(NewIniParam)
    Return NewIniParam
   Else
    ' Section already exists
    Return Nothing
   End If
  End Function
  Protected Friend Sub AddParameter(ByVal p As INI_Parameter)
   mParameters.Add(p)
  End Sub

  Public Function AddParameter(ByVal ParameterName As String, Optional ByVal Value As String = Nothing, Optional ByVal Comment As String = Nothing) As INI_Section
   Dim rt As INI_Parameter = Nothing
   If TryParameter(ParameterName, rt) = False Then
    ' Section doesn't exist
    Dim NewIniParam = New INI_Parameter(Me, ParameterName, , Comment)
    mParameters.Add(NewIniParam)
    Return Me
   Else
    ' Section already exists
    Return Nothing
   End If
  End Function
  Default Public Overloads Property Item(ByVal KeyName As String) As INI_Parameter
   Get
    If KeyName Is Nothing Then Throw New Exceptions_ParameterIsNull("Parameter cannot me null")
    Dim r = From p In mParameters.OfType(Of INI_Parameter)() Select p Where p.Key = KeyName
    If r.Count = 1 Then
     Return r(0)
    Else
     Throw New Exceptions_ParameterNotFound(String.Format("Parameter: [{0}] not found", KeyName))
     Return Nothing
    End If
   End Get
   Set(ByVal value As INI_Parameter)
    If KeyName Is Nothing Then Exit Property
    For i As Integer = 0 To mParameters.Count - 1
     If TypeOf mParameters(i) Is INI_Parameter Then
      If DirectCast(mParameters(i), INI_Parameter).Key = KeyName Then
       mParameters(i) = value
       Exit For
      End If
     End If

    Next
   End Set
  End Property

 End Class
End Namespace


